package com.teamwork.common.handler;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.teamwork.common.exception.BusinessException;
import com.teamwork.common.result.Result;


import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//统一异常处理
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 根据实际自定义的异常统一处理
    @ExceptionHandler(value = BusinessException.class)
    public Result operationError(BusinessException e) {
        return Result.failure("业务异常");
    }

//    @ExceptionHandler(value = RuntimeException.class)
//    public Result operationError(RuntimeException e){
//        return Result.failure(ResultCode.FAIL,e.getMessage());
//    }

    //空指针异常
    @ExceptionHandler(value = NullPointerException.class)
    public Result operationError(NullPointerException e) {
        log.error("出现了空指针异常!");
        return Result.failure("空指针异常");
    }

    //空参数，等其他异常
    @ExceptionHandler(value = MybatisPlusException.class)
    public Result operationError(MybatisPlusException e){
        return Result.failure(e.getMessage());
    }



    //token过期
    @ExceptionHandler(value = ExpiredJwtException.class)
    public Result expireError(ExpiredJwtException e){
      return Result.failure(e.getMessage());
    }

    //运行时异常
    @ExceptionHandler(value = RuntimeException.class)
    public Result runtimeError(RuntimeException e){
        return Result.failure(e.getMessage());
    }

    }

